import { AnyAction, Reducer } from 'redux';
import { EffectsCommandMap } from 'dva';
import {
  queryVIPUserManagementDetail,
  queryCountList,
  queryCountDetail,
  queryExportExcel,
} from './service';

export type Effect = (
  action: AnyAction,
  effects: EffectsCommandMap & { select: <T>(func: (state: {}) => T) => T },
) => void;

export interface ModelType {
  namespace: string;
  state: {};
  effects: {
    fetch: Effect;
    fetchCountList: Effect;
    fetchCountDetail: Effect;
    exportExcel: Effect;
  };
  reducers: {
    save: Reducer;
    saveData: Reducer;
    saveDetailData: Reducer;
  };
}

const Model: ModelType = {
  namespace: 'VIPUserManagementCount',

  state: {
    detailInfo: {},
    dataInfo: [],
    detailCountInfo: [],
  },

  effects: {
    * fetch({ payload }, { call, put }) {
      const response = yield call(queryVIPUserManagementDetail, payload);
      yield put({
        type: 'save',
        payload: response.body,
      });
    },

    * fetchCountList({ payload }, { call, put }) {
      const response = yield call(queryCountList, payload);
      yield put({
        type: 'saveData',
        payload: response.body,
      });
    },

    * fetchCountDetail({ payload }, { call, put }) {
      const response = yield call(queryCountDetail, payload);
      yield put({
        type: 'saveDetailData',
        payload: response.body,
      });
    },

    * exportExcel({ payload }, { call, put }) {
      yield call(queryExportExcel, payload);
    },
  },

  reducers: {
    save(state, action) {
      return {
        ...state,
        detailInfo: action.payload,
      };
    },

    saveData(state, action) {
      return ({
        ...state,
        dataInfo: action.payload,
      });
    },

    saveDetailData(state, action) {
      return ({
        ...state,
        detailCountInfo: action.payload,
      });
    },
  },
};

export default Model;
